package {
import kgame5.kio.ksnake.QueueLoader;
import kgame5.kutil.KEvent;
import kgame5.kutil.T;

import mx.core.ByteArrayAsset;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.utils.ByteArray;

//==================加载皮肤和代码=================
[SWF(width="760",height="600",backgroundColor="0x000000",frameRate="20")]
public class PublishShell extends Sprite{
	
	private var ROOT_URL:String="";
	private var SWF_VER:String="v=1";
	
	private var loadingBar:LoadingBar;
	
	//------1,加载皮肤文件
	private var queueLoader:QueueLoader;
	
	private var 
		sSkinUrl:String,
		sPLAFUrl:String,
		sLinkedItemUrl:String,
		sCodeUrl:String;//TODO 测试时期，不使用加密,以后使用加密方法
		
	public function PublishShell(){
		
		//-提取root_url和swf_Ver
		var P:Object=stage.loaderInfo.parameters;
		if(P.root_url!=null&&P.swf_ver!=null){//如果有flashVar的值，则用flashVar
			ROOT_URL=P.root_url;//加载资源时拼接地址用的
			SWF_VER=P.swf_ver;
			T.p("Shell提取到的FlashVar",ROOT_URL,SWF_VER);
		}
		
		
		
		//-加载图形
		this.startLoad();
	}
	
	
	
	//-------------加载皮肤-------------
	private function startLoad():void{
		
		//-
		loadingBar=new LoadingBar(ROOT_URL+"asset/skin/Loading.swf"+"?"+SWF_VER);
		addChild(loadingBar);
		
		queueLoader=new QueueLoader(ApplicationDomain.currentDomain);
		
		sSkinUrl=ROOT_URL+"asset/skin/SkinMain.swf"+"?"+SWF_VER;
		sPLAFUrl=ROOT_URL+"asset/skin/PLAF.swf"+"?"+SWF_VER;
		sLinkedItemUrl=ROOT_URL+"asset/skin/SkinLinkedItem.swf"+"?"+SWF_VER;
		sCodeUrl=ROOT_URL+"KMain.swf"+"?"+SWF_VER;
		
		queueLoader.addUrl(sSkinUrl,"加载皮肤...");
		queueLoader.addUrl(sPLAFUrl,"加载GUI界面...");
		queueLoader.addUrl(sLinkedItemUrl,"加载链接元件...");
		queueLoader.addUrl(sCodeUrl,"加载主程序...");
		
		
		
		queueLoader.addEventListener(QueueLoader.EVENTTYPE_A_NODE_START, onLoadNodeStart);
		queueLoader.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
		queueLoader.addEventListener(QueueLoader.EVENTTYPE_A_NODE_OK, onLoadNodeOK);
		
		//启动加载
		queueLoader.start(0);
		
		
	}
	
	private function onLoadAllOK():void{
		this.removeChild(loadingBar);
		
		startup();
	}
	
	private function onLoadNodeStart(e:KEvent):void{
		T.p("onLoadNodeStart",e.info);
		loadingBar.setTip(e.info as String);//加载提示字
	}
	//--加载进度条
	private function onLoadProgress(e:ProgressEvent):void{
		//T.p("onLoadProgress",e.bytesLoaded,e.bytesTotal);
		loadingBar.update(e.bytesLoaded, e.bytesTotal);
	}
	private function onLoadNodeOK(e:KEvent):void{
		T.p(this,"onLoadANodeOK");
		if(queueLoader.isAllFinished()){
			onLoadAllOK();
		}
	}
	
	
	
	private function startup():void{
		T.p("startUP,程序启动");
		
		//皮肤
		var	mc_skin:MovieClip=(queueLoader.findNode(sSkinUrl).loader.content) as MovieClip;
		addChild(mc_skin);
		
		var	mc_code:MovieClip=queueLoader.findContent(sCodeUrl) as MovieClip;
		addChild(mc_code);
		mc_code.startup(mc_skin);
	}
}//end of class
}

